package cn.sdormitory.smartclass.service.impl;


import cn.hutool.core.date.DateUtil;
import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.common.utils.ContainerUtil;
import cn.sdormitory.common.utils.StringUtils;
import cn.sdormitory.smartclass.dao.ClassDvDao;
import cn.sdormitory.smartclass.entity.AdClassDv;
import cn.sdormitory.smartclass.entity.ClassDv;
import cn.sdormitory.smartclass.service.ClassDvService;
import cn.sdormitory.smartclass.vo.ClassDvAllExportVO;
import cn.sdormitory.smartclass.vo.ClassDvVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @创建人：zhouyang
 * @创建时间：2021/12/22 16:29
 * @version：V1.0
 */
@Slf4j
@Service("classDvServiceImpl")
public class ClassDvServiceImpl extends ServiceImpl<ClassDvDao, ClassDv> implements ClassDvService {

    @Override
    public int getAppClaStuDvCount(Long classId, String dvDate, String timeSlot) {
        return this.baseMapper.getAppClaStuDvCount(classId,dvDate,timeSlot);
    }

    @Override
    public List<ClassDvVo> getAppClaStuDvByClaIdAndSlot(Long classId, String dvDate, String timeSlot) {
        return this.baseMapper.getAppClaStuDvByClaIdAndSlot(classId,dvDate,timeSlot);
    }

    @Override
    public List<ClassDvVo> getAppClaStuByClaId(Long classId) {
        return this.baseMapper.getAppClaStuByClaId(classId);
    }

    @Override
    public int updateClaDvStatus(String studentNo, String dvDate, String attenceStatus, String timeSlot) {
        return this.baseMapper.updateClaDvStatus(studentNo,dvDate,attenceStatus,timeSlot);
    }

    @Override
    public void addClassDvs(List<ClassDv> classDvs) {
        ContainerUtil.forEach(classDvs,classDv -> this.baseMapper.insert(classDv));
    }

    @Override
    public int addClassDv(AdClassDv adClassDv) {
        int count = 0;
        try {
            String[] studentNoArray = adClassDv.getStudentNoArray();
            Integer[] dvTypeArray = adClassDv.getDvTypeArray();
            if(studentNoArray!=null && studentNoArray.length>0) {
                for (int i = 0; i < studentNoArray.length; i++) {
                    ClassDv classDv = new ClassDv();

                    classDv.setStudentNo(studentNoArray[i]);
                    classDv.setDvDate(adClassDv.getDvDate());
                    classDv.setDvType(dvTypeArray[i] + "");
                    classDv.setClassId(adClassDv.getClassId());
                    classDv.setTimeSlot(adClassDv.getTimeSlot());
                    classDv.setCreate_by(adClassDv.getCreateBy());

                    int dvCount = this.baseMapper.getClaDvByDateAndStuNo(classDv.getStudentNo(),adClassDv.getDvDate(),adClassDv.getTimeSlot());
                    // 根据学号以及违规检查时间判断考勤表中是否有存在的记录
                    if(dvCount<1) {
                        this.baseMapper.insert(classDv);
                    }else{
                        // 根据学号更新违规检查类型
                        this.baseMapper.updateClaDvStatus(classDv.getStudentNo(),classDv.getDvDate(),classDv.getDvType(),classDv.getTimeSlot());
                    }
                }
            }
            count = 1;
        }catch (Exception ex){
            ex.printStackTrace();
            count = 0;
        }

        return  count;
    }

    @Override
    public int createInstructorRecordDv(AdClassDv adClassDv) {
        if("1".equals(adClassDv.getDvArea())){//班级违纪
            if(StringUtils.isEmpty(adClassDv.getTimeSlot())){
                Integer hour = Integer.valueOf(DateUtil.format(new Date(),"HH"));
                if(hour>=8 && hour <12){
                    adClassDv.setTimeSlot("1");
                }else if(hour>=12 && hour <18){
                    adClassDv.setTimeSlot("2");
                }else if(hour>=18 && hour <=21){
                    adClassDv.setTimeSlot("3");
                }
            }
        }
        return this.baseMapper.createInstructorRecordDv(adClassDv);
    }

    @Override
    public List<ClassDvVo> getDvHistoryByUserName(String userName) {
        return baseMapper.getDvHistoryByUserName(userName);
    }

    @Override
    public ClassDvVo getDvDetailById(String id) {
        return baseMapper.getDvDetailById(id);
    }

    @Override
    public CommonPage<ClassDvVo> getPage(Map<String, Object> params) {
        int pageSize = Integer.parseInt(String.valueOf(params.get("pageSize")));
        int pageNum = Integer.parseInt(String.valueOf(params.get("pageNum")));
        if(!org.springframework.util.StringUtils.isEmpty(params.get("dvDates"))){
            List<String> dateList = (List<String>)params.get("dvDates");
            params.put("beginDate",dateList.get(0));
            params.put("endDate",dateList.get(1));
        }
        List<ClassDvVo> list = this.baseMapper.getList(params, (pageNum - 1) * pageSize, pageSize);

        CommonPage commonPage = CommonPage.getCommonPage(pageNum, pageSize, this.baseMapper.getListCount(params), list);

        return commonPage;
    }

    @Override
    public int updatePunishStatus(ClassDvVo vo) {
        return baseMapper.updatePunishStatus(vo);
    }

    @Override
    public List<ClassDvAllExportVO> exportDvData(Map<String, Object> params) {

        return baseMapper.exportDvData(params);
    }

    @Override
    public int countByViolationPatrolId(Long violationPatrolId) {
        return baseMapper.selectCount(Wrappers.lambdaQuery(ClassDv.class)
                .eq(ClassDv::getViolationPatrolId,violationPatrolId)
        );
    }

    @Override
    public List<ClassDv> getClassDvsByViolationPatrolId(Long violationPatrolId) {
        LambdaQueryWrapper<ClassDv> wrapper = Wrappers.lambdaQuery(ClassDv.class).eq(ClassDv::getViolationPatrolId, violationPatrolId);
        return list(wrapper);
    }

    @Override
    public boolean existById(Long dvId) {
        return count(Wrappers.lambdaQuery(ClassDv.class).eq(ClassDv::getId,dvId)) > 0;
    }

    @Override
    public void deleteById(Long dvId) {
        removeById(dvId);
    }
}
